package com.play.ballen.me.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.text.InputType
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.hjq.toast.ToastUtils
import com.play.ballen.R

class InputPayPwdDialogUtils(private val mContext: Context) {

    val dialog: Dialog = Dialog(mContext)
    private lateinit var contentView: View
    private lateinit var edName: EditText

    private val mInputMethodManager by lazy {
        mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    }

    var valuesCallback: ((text: String) -> Unit)? = null

    fun show(callback: ((text: String) -> Unit)) {
        this.valuesCallback = callback
        if (!this::contentView.isInitialized) {
            contentView =
                LayoutInflater.from(mContext).inflate(R.layout.dialog_input_pay_pwd, null)
            contentView.let {
                edName = it.findViewById(R.id.edName)

                it.findViewById<ImageView>(R.id.imgClose).setOnClickListener {
                    dialog.dismiss()
                }
                it.findViewById<CheckBox>(R.id.cbPwdVisible)
                    .setOnCheckedChangeListener { buttonView, isChecked ->
                        if (isChecked) {
                            edName.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                        } else {
                            edName.inputType =
                                InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                        }
                        edName.setSelection(edName.text.toString().trim().length)
                    }
                it.findViewById<TextView>(R.id.btnOk).setOnClickListener {
                    if (edName.text.toString().isEmpty()) {
                        ToastUtils.show(edName.hint.toString())
                        return@setOnClickListener
                    }


                    val name = edName.text.toString()
                    dialog.dismiss()
                    valuesCallback?.invoke(name)
                }

                dialog.setContentView(it)
                dialog.setCanceledOnTouchOutside(false)
                dialog.window?.setLayout(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT
                )
                dialog.window?.setBackgroundDrawable(ColorDrawable())
                dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
                dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
            }
        }

        if (!dialog.isShowing) {
            dialog.show()

            edName.post {
                edName.requestFocus()
                mInputMethodManager.showSoftInput(edName, InputMethodManager.SHOW_IMPLICIT)
            }
        }
    }

}